package com.steel.yfmall.cloud.assistant.goods.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.steel.yfmall.cloud.assistant.goods.ProductPriceFormFactorService;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductPriceFormBean;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductPriceFormFactorDatailsBean;
import com.steel.yfmall.cloud.assistant.dto.result.goods.ResultProductPriceFormFactorDatailsBean;
import com.steel.yfmall.cloud.assistant.dto.zg.price.Sr063Bean;
import com.steel.yfmall.cloud.assistant.goods.mapper.SteelProductPriceFormBeanMapper;
import com.steel.yfmall.cloud.assistant.goods.mapper.SteelProductPriceFormFactorDatailsBeanMapper;
import com.steel.yfmall.cloud.assistant.po.goods.*;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: ProductPriceFormFactorServiceImpl
 * @Package com.steel.yfmall.cloud.assistant.biz.service.goods.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2020/12/7 21:33
 */
@Service
@Slf4j
public class ProductPriceFormFactorServiceImpl implements ProductPriceFormFactorService {

    @Resource
    private SteelProductPriceFormFactorDatailsBeanMapper steelProductPriceFormFactorDatailsBeanMapper;

    @Resource
    private SteelProductPriceFormBeanMapper steelProductPriceFormBeanMapper;

    /**
     * 查询价格因子信息
     * @Title: getSteelProductPriceFormFactorDatailsBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return com.steel.yfmall.cloud.assistant.po.goods.SteelProductPriceFormFactorDatailsBean    返回类型
     * @author yuchaohua
     * @date 2020/12/7 21:56
     */
    @Override
    public SteelProductPriceFormFactorDatailsBean getSteelProductPriceFormFactorDatailsBeanByPara(ParaProductPriceFormFactorDatailsBean paraBean) {
        SteelProductPriceFormFactorDatailsBeanExample example = new SteelProductPriceFormFactorDatailsBeanExample();
        SteelProductPriceFormFactorDatailsBeanExample.Criteria criteria = example.createCriteria();
        getProductPriceFormFactorSearchDate(paraBean, criteria);
        return steelProductPriceFormFactorDatailsBeanMapper.selectOneByExampleSelective(example);
    }

    /**
     * 获取价格因子集合
     * @Title: getSteelProductPriceFormFactorDetailsBeanListByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.List<com.steel.yfmall.cloud.assistant.po.goods.SteelProductPriceFormFactorDatailsBean>    返回类型
     * @author yuchaohua
     * @date 2020/12/8 17:15
     */
    @Override
    public List<ResultProductPriceFormFactorDatailsBean> getSteelProductPriceFormFactorDetailsBeanListByPara(ParaProductPriceFormFactorDatailsBean paraBean) {
        List<ResultProductPriceFormFactorDatailsBean> returnBean = null;
        SteelProductPriceFormFactorDatailsBeanExample example = new SteelProductPriceFormFactorDatailsBeanExample();
        SteelProductPriceFormFactorDatailsBeanExample.Criteria criteria = example.createCriteria();
        getProductPriceFormFactorSearchDate(paraBean, criteria);
        List<SteelProductPriceFormFactorDatailsBean> dbResultList = steelProductPriceFormFactorDatailsBeanMapper.selectByExampleWithBLOBs(example);
        if(CollectionUtils.isNotEmpty(dbResultList)){
            returnBean = new ArrayList<>();
            for(SteelProductPriceFormFactorDatailsBean itemBean:dbResultList){
                ResultProductPriceFormFactorDatailsBean resultSteelProductPriceFormFactorDatailsBean = new ResultProductPriceFormFactorDatailsBean();
                MobileBaseBeanUtils.copyAllFields(itemBean,resultSteelProductPriceFormFactorDatailsBean);
                //封装价格因子匹配数据
                if(StringUtils.isNotBlank(itemBean.getProductPriceFormFactorDetail())){
                    List<Sr063Bean> productPriceFormFactorList = null;
                    try{
                        productPriceFormFactorList= JSONArray.parseArray(itemBean.getProductPriceFormFactorDetail(),Sr063Bean.class);
                    }catch (Exception e){
                        log.error("价格组因子信息转换失败:" + JSON.toJSONString(itemBean.getProductPriceFormFactorDetail()));
                    }
                    resultSteelProductPriceFormFactorDatailsBean.setProductPriceFormFactorDetailList(productPriceFormFactorList);
                }
                returnBean.add(resultSteelProductPriceFormFactorDatailsBean);
            }
        }
        return returnBean;

    }

    /**
     * 获取价格组的价格因子集合 key价格组ID
     * @Title: getSteelProductPriceFormFactorDetailsBeanMap
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.Map<java.lang.Integer,java.util.List<com.steel.yfmall.cloud.assistant.po.goods.SteelProductPriceFormFactorDatailsBean>>    返回类型
     * @author yuchaohua
     * @date 2020/12/8 17:16
     */
    @Override
    public Map<Integer, List<ResultProductPriceFormFactorDatailsBean>> getSteelProductPriceFormFactorDetailsBeanMap(ParaProductPriceFormFactorDatailsBean paraBean) {
        Map<Integer, List<ResultProductPriceFormFactorDatailsBean>> returnBean = null;
        List<ResultProductPriceFormFactorDatailsBean> dbResultList = getSteelProductPriceFormFactorDetailsBeanListByPara(paraBean);
        if(dbResultList != null && dbResultList.size() > 0){
            returnBean = new HashMap<>();
            for(ResultProductPriceFormFactorDatailsBean iteaBean:dbResultList){
                if(returnBean.containsKey(iteaBean.getProductPriceFormId())){
                    returnBean.get(iteaBean.getProductPriceFormId()).add(iteaBean);
                }else{
                    List<ResultProductPriceFormFactorDatailsBean>  priceFormFactorDetailsBeanList = new ArrayList<>();
                    priceFormFactorDetailsBeanList.add(iteaBean);
                    returnBean.put(iteaBean.getProductPriceFormId(),priceFormFactorDetailsBeanList);
                }
            }
        }
        return returnBean;
    }


    /**
     * 新增价格因子信息
     * @Title: batchInsertSteelProductPriceFormFactorDatailsBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param recordList 参数说明
     * @return boolean    返回类型
     * @author yuchaohua
     * @date 2020/12/7 21:56
     */
    @Override
    public boolean batchInsertSteelProductPriceFormFactorDatailsBeanByPara(List<SteelProductPriceFormFactorDatailsBean> recordList) {
        if (CollectionUtils.isEmpty(recordList)) {
            return false;
        }
        return steelProductPriceFormFactorDatailsBeanMapper.batchInsertByList(recordList) > 0;
    }

    /**
     * 删除价格因子信息
     * @Title: removeSteelProductPriceFormFactorDatailsBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return boolean    返回类型
     * @author yuchaohua
     * @date 2020/12/7 22:00
     */
    @Override
    public boolean removeSteelProductPriceFormFactorDatailsBeanByPara(ParaProductPriceFormFactorDatailsBean paraBean) {
        if (paraBean == null || CollectionUtils.isEmpty(paraBean.getProductPriceFormIdList())) {
            return false;
        }
        SteelProductPriceFormFactorDatailsBeanExample example = new SteelProductPriceFormFactorDatailsBeanExample();
        SteelProductPriceFormFactorDatailsBeanExample.Criteria criteria = example.createCriteria();
        getProductPriceFormFactorSearchDate(paraBean, criteria);
        return steelProductPriceFormFactorDatailsBeanMapper.deleteByExample(example) > 0;
    }

    /**
     * 价格主表信息sql条件拼接
     *
     * @param paraBean
     * @param criteria
     */
    private void getProductPriceFormFactorSearchDate(ParaProductPriceFormFactorDatailsBean paraBean, SteelProductPriceFormFactorDatailsBeanExample.Criteria criteria) {
        if (paraBean == null || criteria == null) {
            return;
        }
        if (StringUtils.isNotBlank(paraBean.getJyjtProductPriceFormId())) {
            criteria.andJyjtProductPriceFormIdEqualTo(paraBean.getJyjtProductPriceFormId());
        }
        if (paraBean.getProductPriceFormId() != null) {
            criteria.andProductPriceFormIdEqualTo(paraBean.getProductPriceFormId());
        }
        if (CollectionUtils.isNotEmpty(paraBean.getProductPriceFormIdList())){
            criteria.andProductPriceFormIdIn(paraBean.getProductPriceFormIdList());
        }
        if (null != paraBean.getId()) {
            criteria.andIdEqualTo(paraBean.getId());
        }
    }

    /**
     * 获取价格组成
     * @Title: getSteelProductPriceFormBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.List<com.steel.yfmall.cloud.assistant.po.goods.SteelProductPriceFormBean>    返回类型
     * @author yuchaohua
     * @date 2020/12/7 22:05
     */
    @Override
    public List<SteelProductPriceFormBean> getSteelProductPriceFormBeanByPara(ParaProductPriceFormBean paraBean) {
        SteelProductPriceFormBeanExample example = new SteelProductPriceFormBeanExample();
        SteelProductPriceFormBeanExample.Criteria criteria = example.createCriteria();
        getProductPriceFormSearchDate(paraBean, criteria);
        return steelProductPriceFormBeanMapper.selectByExampleWithBLOBs(example);
    }

    /**
     * 新增价格组成信息
     * @Title: insertSteelProductPriceFormBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param recordBean 参数说明
     * @return int    返回类型
     * @author yuchaohua
     * @date 2020/12/7 22:17
     */
    @Override
    public int insertSteelProductPriceFormBeanByPara(SteelProductPriceFormBean recordBean) {
        if (recordBean == null || StringUtils.isBlank(recordBean.getPriceClassify()) || StringUtils.isBlank(recordBean.getPriceType())  || StringUtils.isBlank(recordBean.getPriceCode())) {
            return 0;
        }
        return steelProductPriceFormBeanMapper.insertSelectiveReturnId(recordBean);
    }

    /**
     * 删除价格组成信息
     * @Title: removeSteelProductPriceFormBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return boolean    返回类型
     * @author yuchaohua
     * @date 2020/12/7 22:21
     */
    @Override
    public boolean removeSteelProductPriceFormBeanByPara(ParaProductPriceFormBean paraBean) {
        if (paraBean == null || StringUtils.isBlank(paraBean.getPriceClassify()) || StringUtils.isBlank(paraBean.getPriceType()) ) {
            return false;
        }
        boolean flag = false;
        List<SteelProductPriceFormBean> resultPriceFormList = getSteelProductPriceFormBeanByPara(paraBean);
        if (CollectionUtils.isNotEmpty(resultPriceFormList)){
            // 价格组成ID集合
            List<Integer> productPriceFormIdList = new ArrayList<>();
            for(SteelProductPriceFormBean itemBean : resultPriceFormList){
                productPriceFormIdList.add(itemBean.getId());
            }
            SteelProductPriceFormBeanExample example = new SteelProductPriceFormBeanExample();
            SteelProductPriceFormBeanExample.Criteria criteria = example.createCriteria();
            getProductPriceFormSearchDate(paraBean, criteria);
            flag = steelProductPriceFormBeanMapper.deleteByExample(example) > 0;
            // 如果成功了将价格因子也删除掉
            if (flag){
                ParaProductPriceFormFactorDatailsBean paraProductPriceFormFactorDatailsBean = new ParaProductPriceFormFactorDatailsBean();
                paraProductPriceFormFactorDatailsBean.setProductPriceFormIdList(productPriceFormIdList);
                removeSteelProductPriceFormFactorDatailsBeanByPara(paraProductPriceFormFactorDatailsBean);
            }
        }
        return flag;
    }

    /**
     * 价格组成SQL拼接
     * @param paraBean
     * @param criteria
     */
    private void getProductPriceFormSearchDate(ParaProductPriceFormBean paraBean, SteelProductPriceFormBeanExample.Criteria criteria) {
        if (paraBean == null || criteria == null) {
            return;
        }
        if (StringUtils.isNotBlank(paraBean.getPriceType())) {
            criteria.andPriceTypeEqualTo(paraBean.getPriceType());
        }
        if (StringUtils.isNotBlank(paraBean.getPriceClassify())) {
            criteria.andPriceClassifyEqualTo(paraBean.getPriceClassify());
        }
        if (StringUtils.isNotBlank(paraBean.getPriceCode())) {
            criteria.andPriceCodeEqualTo(paraBean.getPriceCode());
        }
        if (null != paraBean.getId()) {
            criteria.andIdEqualTo(paraBean.getId());
        }
        if (null != paraBean.getStatus()) {
            criteria.andStatusEqualTo(paraBean.getStatus());
        }
    }


}
